package com.zz.fileoperatelib.loadmanager;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;

import androidx.fragment.app.FragmentActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zhuozhang6
 * @date: 2021/12/29
 * @email: zhuozhang6@iflytek.com
 * @Description:
 */
public class LoadLocalPhoto {
    //读取图片文件的参数
    private static final String STORE_IMAGES[] = {
            MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.DISPLAY_NAME,
            MediaStore.Images.Media.DATE_ADDED,
            MediaStore.Images.Media._ID
    };

    public FragmentActivity activity;
    private LoadFinishCallback loadFinishCallback;

    //构造方法传入当前的Activity
    public LoadLocalPhoto(FragmentActivity activity) {
        this.activity = activity;
        activity.getSupportLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() {

            @Override
            public Loader<Cursor> onCreateLoader(int id, Bundle args) {
                CursorLoader cursorLoader = new CursorLoader(activity, MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        STORE_IMAGES, null, null, STORE_IMAGES[2] + " DESC");
                return cursorLoader;
            }

            //加载完成调用此方法
            @Override
            public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
                if (data == null) {
                    return;
                }
                List<ImageFolder> folderList = new ArrayList<>();
                int count = data.getCount();
                if (count > 0) {
                    data.moveToFirst();
                    do {
                        String path = data.getString(data.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
                        File imageFile = new File(path);
                        File folderFile = imageFile.getParentFile();
                        ImageFolder folder = new ImageFolder();
                        folder.path = folderFile.getAbsolutePath();
                        folder.name = folderFile.getName();
                        if (!folderList.contains(folder)) {
                            List<String> imageList = new ArrayList<>();
                            imageList.add(path);
                            folder.images = imageList;
                            folderList.add(folder);
                        } else {
                            ImageFolder folder1 = folderList.get(folderList.indexOf(folder));
                            folder1.images.add(path);
                        }
                    } while (data.moveToNext());
                }
                //接口调用
                loadFinishCallback.finish(folderList);
            }



            @Override
            public void onLoaderReset(Loader<Cursor> loader) {

            }
        });
    }
    public void setLoadFinishCallback(LoadFinishCallback loadFinishCallback) {
        this.loadFinishCallback = loadFinishCallback;
    }

    public interface LoadFinishCallback {
        void finish(List<ImageFolder> folderList);
    }
}